package com.six.park.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.six.park.service.ParkService;

import net.sf.json.JSONArray;
import com.six.park.parking.IN;
import com.six.park.parking.OUT;

@Controller
@RequestMapping("controller")
public class ParkController {
	@Autowired
	ParkService pService;

	//定义一个map接收
	public static Map<Object, String> map2 = new HashMap<Object, String>();

	public ParkController() {
		System.out.println("ParkController构造");
	}

	// 登录
	@RequestMapping(value = "/login", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String login(@RequestParam Map<String, String> map, HttpServletRequest request,
						HttpServletResponse response) {
		System.out.println("controller方法---------login");
		//session对象
		HttpSession session = request.getSession();
		//获取前台输入的帐号密码
		String uname = map.get("name");
		String pwd = map.get("pwd");

		Map map3 = new HashMap();
		map3.put("uname", uname);
		map3.put("pwd", pwd);
		Map rs = pService.login(map3);
		Map rs2 = pService.login2(map3);

		System.out.println(map3);
		session.setAttribute("Name", uname);
		System.out.println(rs);
		if(rs != null) {
            session.setAttribute("Name", uname);
			Object ss = session.getAttribute("Name");
			System.out.println(session.getAttribute("Name"));
			if(map2.containsValue(ss))
			{
				System.out.println("已登录");
				return "{\"aa\":\"已登录\"}";
			}else
			{
                session.setAttribute("Name", uname);
				System.out.println("admin登录成功");
				map2.put(session.getId(),uname.toString());
				System.out.println("添加后的map： "+map2);
				return "{\"aa\":\"admin\"}";
			}

		}else if(rs2!=null){
			Object ss = session.getAttribute("Name");
			System.out.println(session.getAttribute("Name"));
			if(map2.containsValue(ss))
			{
				System.out.println("已登录");
				return "{\"aa\":\"已登录\"}";
			}else
			{
				session.setAttribute("garageName",rs2.get("garageName"));
				System.out.println("manager    "+rs2);
				System.out.println("管理员登录成功");
				map2.put(session.getId(),uname.toString());
				System.out.println("添加后的map： "+map2);
				return "{\"aa\":\"manager\"}";
			}
		}else {
			return "{\"aa\":\"登录失败\"}";

		}
	}

	//userName
	@RequestMapping(value="/userName",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String username(@RequestParam Map<String, String> map2,HttpServletRequest request,HttpServletResponse response) {
		System.out.println("username.....");
		//session对象
		HttpSession session = request.getSession();
		String username = session.getAttribute("Name").toString();
		return username;
	}

	//garageName
	@RequestMapping(value="/garageName",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String garageName(@RequestParam Map<String, String> map2,HttpServletRequest request,HttpServletResponse response) {
		System.out.println("garageName.....");
		//session对象
		HttpSession session = request.getSession();
		String garageName = session.getAttribute("garageName").toString();
		return garageName;
	}

	//log日志
	@RequestMapping(value="/log",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String log(@RequestParam Map<String, String> map2,HttpServletRequest request,HttpServletResponse response) {
		System.out.println("log日志.....");
		List<Map> list = pService.log();
		// 设置字符编码集
		response.setContentType("text/text"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");
		if (list != null) {
			System.out.println("日志数据获取成功");

			JSONArray json = JSONArray.fromObject(list);
//			System.out.println("json   " + json);
			// 将json对象转换为字符串
			String data = json.toString();
//			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("日志数据获取失败");
		}
		return null;


	}

	//log日志
	@RequestMapping(value="/history2",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String log2(@RequestParam Map<String, String> map2,HttpServletRequest request,HttpServletResponse response) {
		System.out.println("log日志2.....");

		Map map = new HashMap();
		//session对象
		HttpSession session = request.getSession();
		map.put("garageName",session.getAttribute("garageName"));

		List<Map> list = pService.log2(map);
		// 设置字符编码集
		response.setContentType("text/text"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");
		if (list != null) {
			System.out.println("日志2数据获取成功");

			JSONArray json = JSONArray.fromObject(list);
//			System.out.println("json   " + json);
			// 将json对象转换为字符串
			String data = json.toString();
//			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("日志2数据获取失败");
		}
		return null;


	}

	// 分配的车位，时间信息查询
	@RequestMapping(value = "/start", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String Chewei(@RequestParam Map<String, String> maps, HttpServletRequest request,
						 HttpServletResponse response) throws Exception {
		System.out.println("添加车牌");
		// 设置字符编码集
		response.setContentType("text/text"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");

		String carname = IN.aaa();
		Map map = new HashMap();
		map.put("carname", carname);
//		System.out.println(map);
		System.out.println("增加成功");
		System.out.println("添加起始信息");

		// 设置起始时间
		java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 这个是设置时间的格式;
		Date currentTime = new java.util.Date();// 得到当前系统时间
		String str_date1 = df.format(currentTime); // 将日期时间格式化
		System.out.println(str_date1);
		Map maptable = new HashMap();
		maptable.put("table","dt1");

		//session对象
		HttpSession session = request.getSession();
		String garageName = session.getAttribute("garageName").toString();

		List<Map> list;
		if(garageName.equals("DT1")){
			Map cheWeiSelect = pService.cheWeiSelect1(maptable); // 分配的车位
//			System.out.println(cheWeiSelect);

			if (cheWeiSelect == null) {
				System.out.println("分配车位失败");
				return "失败";
			} else {
				System.out.println("分配车位成功");

				Map mapc = new HashMap();
				mapc.put("carname", carname);
				mapc.put("str_date1", str_date1);
				mapc.putAll(cheWeiSelect);
				// 修改起始信息
				int starts = pService.cheWeiUpdate1(mapc);
				System.out.println(starts);
				if (starts == 0) {
					System.out.println("修改信息失败");
				} else {
					System.out.println("修改信息成功 ");
					Map map2 = new HashMap();
					map2.put("carname", carname);
					List<Map> cheWei = pService.cheWei1(map2);
					JSONArray json = JSONArray.fromObject(cheWei);
					System.out.println("json   " + json);
					// 将json对象转换为字符串
					String data = json.toString();
					System.out.println("data    " + data);
					return data;
				}
			}
		}else if(garageName.equals("DT2")){
			Map cheWeiSelect = pService.cheWeiSelect2(maptable); // 分配的车位
			System.out.println(cheWeiSelect);

			if (cheWeiSelect == null) {
				System.out.println("分配车位失败");
				return "失败";
			} else {
				System.out.println("分配车位成功");

				Map mapc = new HashMap();
				mapc.put("carname", carname);
				mapc.put("str_date1", str_date1);
				mapc.putAll(cheWeiSelect);
				// 修改起始信息
				int starts = pService.cheWeiUpdate2(mapc);
				System.out.println(starts);
				if (starts == 0) {
					System.out.println("修改信息失败");
				} else {
					System.out.println("修改信息成功 ");
					Map map2 = new HashMap();
					map2.put("carname", carname);
					List<Map> cheWei = pService.cheWei2(map2);
					JSONArray json = JSONArray.fromObject(cheWei);
					System.out.println("json   " + json);
					// 将json对象转换为字符串
					String data = json.toString();
					System.out.println("data    " + data);
					return data;
				}
			}
		}else{
			Map cheWeiSelect = pService.cheWeiSelect3(maptable); // 分配的车位
			System.out.println(cheWeiSelect);

			if (cheWeiSelect == null) {
				System.out.println("分配车位失败");
				return "失败";
			} else {
				System.out.println("分配车位成功");

				Map mapc = new HashMap();
				mapc.put("carname", carname);
				mapc.put("str_date1", str_date1);
				mapc.putAll(cheWeiSelect);
				// 修改起始信息
				int starts = pService.cheWeiUpdate3(mapc);
				System.out.println(starts);
				if (starts == 0) {
					System.out.println("修改信息失败");
				} else {
					System.out.println("修改信息成功 ");
					Map map2 = new HashMap();
					map2.put("carname", carname);
					List<Map> cheWei = pService.cheWei3(map2);
					JSONArray json = JSONArray.fromObject(cheWei);
					System.out.println("json   " + json);
					// 将json对象转换为字符串
					String data = json.toString();
					System.out.println("data    " + data);
					return data;
				}
			}
		}

		return null;

	}

	// 车辆全查询
	@RequestMapping(value = "/end", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String money(HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("添加结束信息");
		// 设置字符编码集
		response.setContentType("text/text"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");
		Map map = new HashMap();

		// 获取车牌
		String carname = OUT.aaa();
		System.out.println("车牌    " + carname);
		map.put("carname", carname);
		//session对象
		HttpSession session = request.getSession();
		String garageName = session.getAttribute("garageName").toString();

		List<Map> list;
		if(garageName.equals("DT1")){
			Map inTime = pService.inTime1(map);
			if (inTime == null) {
				System.out.println("获取起始时间失败");
			} else {
				System.out.println("获取起始时间成功");
				System.out.println("进入时间map" + inTime);
				String beginTime = (String) inTime.get("entry_time");

				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				// 这个是设置时间的格式;
				Date currentTime = new Date();// 得到当前系统时间
				String str_date1 = df.format(currentTime); // 将日期时间格式化
				System.out.println("离开车库的时间为:" + str_date1);
				Date time1 = df.parse(beginTime);// 转化为date类型
				Date time2 = df.parse(str_date1);// 转化为date类型
				System.out.println(time1 + "\t" + time2);
				System.out.println(str_date1);
				// 将date类型的时间转为long类型
				long l = time2.getTime() - time1.getTime();
				long day = l / (24 * 60 * 60 * 1000);
				long hour = (l / (60 * 60 * 1000) - day * 24);
				long min = (l / (60 * 1000) - day * 24 * 60 - hour * 60);
				long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
				System.out.println("您的车一共停放了:" + day + "天" + hour + "小时" + min + "分" + s + "秒");
				String str = day + "天" + hour + "小时" + min + "分";
				System.out.println("共停放了:" + str);
				long sum = 0;
				if (day >= 1) {
					if (min >= 30) {
						sum = day * 24 * 3 + hour * 3 + 2 * 2;

					} else {
						sum = day * 24 * 3 + hour * 3 + 1 * 2;
					}
				} else {
					if (min >= 30) {
						sum = day * 24 * 2 * 2 + hour * 2 * 2 + 2 * 2;

					} else {
						sum = day * 24 * 2 * 2 + hour * 2 * 2 + 1 * 2;
					}
				}

				System.out.println("应付停车费:" + sum + "元");
				// 把需要支付的停车费添加到数据库
				Map map2 = new HashMap();
				map2.put("str_date1", str_date1);
				map2.put("str", str);
				map2.put("sum", sum);
				map2.put("carname", carname);
				int jieSuan = pService.jieSuan2(map2);
				if (jieSuan == 0) {
					System.out.println("结算失败");
				} else {
					System.out.println("结算成功");
					List<Map> all = pService.cheWei1(map);
					if (all == null) {
						System.out.println("获取所有信息失败");
					} else {
						System.out.println("获取所有信息成功");
						JSONArray json = JSONArray.fromObject(all);
						System.out.println("json     ." + json);
						String data = json.toString();
						System.out.println("data      .." + data);
						return data;
					}
				}
			}
		}else if(garageName.equals("DT2")){
			Map inTime = pService.inTime2(map);
			if (inTime == null) {
				System.out.println("获取起始时间失败");
			} else {
				System.out.println("获取起始时间成功");
				System.out.println("进入时间map" + inTime);
				String beginTime = (String) inTime.get("entry_time");

				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				// 这个是设置时间的格式;
				Date currentTime = new Date();// 得到当前系统时间
				String str_date1 = df.format(currentTime); // 将日期时间格式化
				System.out.println("离开车库的时间为:" + str_date1);
				Date time1 = df.parse(beginTime);// 转化为date类型
				Date time2 = df.parse(str_date1);// 转化为date类型
				System.out.println(time1 + "\t" + time2);
				System.out.println(str_date1);
				// 将date类型的时间转为long类型
				long l = time2.getTime() - time1.getTime();
				long day = l / (24 * 60 * 60 * 1000);
				long hour = (l / (60 * 60 * 1000) - day * 24);
				long min = (l / (60 * 1000) - day * 24 * 60 - hour * 60);
				long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
				System.out.println("您的车一共停放了:" + day + "天" + hour + "小时" + min + "分" + s + "秒");
				String str = day + "天" + hour + "小时" + min + "分";
				System.out.println("共停放了:" + str);
				long sum = 0;
				if (day >= 1) {
					if (min >= 30) {
						sum = day * 24 * 3 + hour * 3 + 2 * 2;

					} else {
						sum = day * 24 * 3 + hour * 3 + 1 * 2;
					}
				} else {
					if (min >= 30) {
						sum = day * 24 * 2 * 2 + hour * 2 * 2 + 2 * 2;

					} else {
						sum = day * 24 * 2 * 2 + hour * 2 * 2 + 1 * 2;
					}
				}

				System.out.println("应付停车费:" + sum + "元");
				// 把需要支付的停车费添加到数据库
				Map map2 = new HashMap();
				map2.put("str_date1", str_date1);
				map2.put("str", str);
				map2.put("sum", sum);
				map2.put("carname", carname);
				int jieSuan = pService.jieSuan2(map2);
				if (jieSuan == 0) {
					System.out.println("结算失败");
				} else {
					System.out.println("结算成功");
					List<Map> all = pService.cheWei2(map);
					if (all == null) {
						System.out.println("获取所有信息失败");
					} else {
						System.out.println("获取所有信息成功");
						JSONArray json = JSONArray.fromObject(all);
						System.out.println("json     ." + json);
						String data = json.toString();
						System.out.println("data      .." + data);
						return data;
					}
				}
			}
		}else{
			Map inTime = pService.inTime3(map);
			if (inTime == null) {
				System.out.println("获取起始时间失败");
			} else {
				System.out.println("获取起始时间成功");
				System.out.println("进入时间map" + inTime);
				String beginTime = (String) inTime.get("entry_time");

				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				// 这个是设置时间的格式;
				Date currentTime = new Date();// 得到当前系统时间
				String str_date1 = df.format(currentTime); // 将日期时间格式化
				System.out.println("离开车库的时间为:" + str_date1);
				Date time1 = df.parse(beginTime);// 转化为date类型
				Date time2 = df.parse(str_date1);// 转化为date类型
				System.out.println(time1 + "\t" + time2);
				System.out.println(str_date1);
				// 将date类型的时间转为long类型
				long l = time2.getTime() - time1.getTime();
				long day = l / (24 * 60 * 60 * 1000);
				long hour = (l / (60 * 60 * 1000) - day * 24);
				long min = (l / (60 * 1000) - day * 24 * 60 - hour * 60);
				long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
				System.out.println("您的车一共停放了:" + day + "天" + hour + "小时" + min + "分" + s + "秒");
				String str = day + "天" + hour + "小时" + min + "分";
				System.out.println("共停放了:" + str);
				long sum = 0;
				if (day >= 1) {
					if (min >= 30) {
						sum = day * 24 * 3 + hour * 3 + 2 * 2;

					} else {
						sum = day * 24 * 3 + hour * 3 + 1 * 2;
					}
				} else {
					if (min >= 30) {
						sum = day * 24 * 2 * 2 + hour * 2 * 2 + 2 * 2;

					} else {
						sum = day * 24 * 2 * 2 + hour * 2 * 2 + 1 * 2;
					}
				}

				System.out.println("应付停车费:" + sum + "元");
				// 把需要支付的停车费添加到数据库
				Map map2 = new HashMap();
				map2.put("str_date1", str_date1);
				map2.put("str", str);
				map2.put("sum", sum);
				map2.put("carname", carname);
				int jieSuan = pService.jieSuan3(map2);
				if (jieSuan == 0) {
					System.out.println("结算失败");
				} else {
					System.out.println("结算成功");
					List<Map> all = pService.cheWei3(map);
					if (all == null) {
						System.out.println("获取所有信息失败");
					} else {
						System.out.println("获取所有信息成功");
						JSONArray json = JSONArray.fromObject(all);
						System.out.println("json     ." + json);
						String data = json.toString();
						System.out.println("data      .." + data);
						return data;
					}
				}
			}
		}


		return null;

	}

	// 删除车牌，删加车位，删除图片
	@RequestMapping(value = "/delImg", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String delImg(@RequestParam Map map, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Delete_pic del = new Delete_pic();

		String carname = OUT.aaa();
		System.out.println("车牌为：" + carname);
		map.put("carname", carname);

		//session对象
		HttpSession session = request.getSession();
		String garageName = session.getAttribute("garageName").toString();

		List<Map> list;
		if(garageName.equals("DT1")){
			//查询出去的车的id，状态码，根据车牌查询
			Map rs = pService.select1(map);
			if (rs != null) {
				System.out.println("查询出去的车的id，状态码:"+rs);

				//删除已走车的车位（根据车牌删除）
				int deleteCar = pService.deleteCar1(map);
				if(deleteCar != 0) {
					System.out.println("车位里的车删除成功");

					//添加已走车的车位（根据编号和状态码添加）
					int insert = pService.insert1(rs);
					if(insert != 0) {
						System.out.println("添加车位成功");

						return "{\"aa\":\"添加车位成功\"}";

					}else {
						System.out.println("添加失败");
					}
				}else {
					System.out.println("删除失败");
				}
			} else {
				System.out.println("查询失败");
			}
		}else if(garageName.equals("DT2")){
			//查询出去的车的id，状态码，根据车牌查询
			Map rs = pService.select2(map);
			if (rs != null) {
				System.out.println("查询出去的车的id，状态码:"+rs);

				//删除已走车的车位（根据车牌删除）
				int deleteCar = pService.deleteCar2(map);
				if(deleteCar != 0) {
					System.out.println("车位里的车删除成功");

					//添加已走车的车位（根据编号和状态码添加）
					int insert = pService.insert2(rs);
					if(insert != 0) {
						System.out.println("添加车位成功");
						return "{\"aa\":\"添加车位成功\"}";
					}else {
						System.out.println("添加失败");
					}
				}else {
					System.out.println("删除失败");
				}
			} else {
				System.out.println("查询失败");
			}
		}else{
			//查询出去的车的id，状态码，根据车牌查询
			Map rs = pService.select3(map);
			if (rs != null) {
				System.out.println("查询出去的车的id，状态码:"+rs);

				//删除已走车的车位（根据车牌删除）
				int deleteCar = pService.deleteCar3(map);
				if(deleteCar != 0) {
					System.out.println("车位里的车删除成功");

					//添加已走车的车位（根据编号和状态码添加）
					int insert = pService.insert3(rs);
					if(insert != 0) {
						System.out.println("添加车位成功");
						return "{\"aa\":\"添加车位成功\"}";
					}else {
						System.out.println("添加失败");
					}
				}else {
					System.out.println("删除失败");
				}
			} else {
				System.out.println("查询失败");
			}
		}

		return null;

	}

	// 注销
	@RequestMapping(value = "/distroy", method = RequestMethod.GET)
	@ResponseBody
	public String distroy1(HttpServletRequest request, Model model) {
		System.out.println("-----首页注销-----");
		HttpSession session = request.getSession();
		System.out.println("这是注销的map");
		System.out.println("注销前：" + map2);
		session.setAttribute("Name",null);
		// map.remove(session.getId());
		// map.remove(session.getAttribute("Session_id"));
		map2.remove(session.getId());
		System.out.println("注销后的map ：" + map2);
		session.invalidate();
		// req.getRequestDispatcher("/JSP/login.jsp").forward(req, resp);
		return "cg";
	}

	// DT1车库
	@RequestMapping(value = "/DT1", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String dt1(@RequestParam Map<String, String> map, HttpServletRequest request,
					   HttpServletResponse response) {
		System.out.println("DT1车库.....");
		List<Map> list = pService.dt1();
		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");
		if (list != null) {
			System.out.println("DT1车库数据获取成功");

			JSONArray json = JSONArray.fromObject(list);
			System.out.println("json   " + json);
			// 将json对象转换为字符串
			String data = json.toString();
			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("DT1车库数据获取失败");
		}
		return null;

	}

	// DT2车库
	@RequestMapping(value = "/DT2", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String dt2(@RequestParam Map<String, String> map, HttpServletRequest request,
					   HttpServletResponse response) {
		System.out.println("DT2车库.....");
		List<Map> list = pService.dt2();
		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");
		if (list != null) {
			System.out.println("DT2车库数据获取成功");

			JSONArray json = JSONArray.fromObject(list);
			System.out.println("json   " + json);
			// 将json对象转换为字符串
			String data = json.toString();
			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("DT2车库数据获取失败");
		}
		return null;

	}

	// DT3车库
	@RequestMapping(value = "/DT3", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String dt3(@RequestParam Map<String, String> map, HttpServletRequest request,
					   HttpServletResponse response) {
		System.out.println("DT3车库.....");
		List<Map> list = pService.dt3();
		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");
		if (list != null) {
			System.out.println("DT3车库数据获取成功");

			JSONArray json = JSONArray.fromObject(list);
			System.out.println("json   " + json);
			// 将json对象转换为字符串
			String data = json.toString();
			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("DT3车库数据获取失败");
		}
		return null;

	}

	// table
	@RequestMapping(value = "/table", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String table(@RequestParam Map<String, String> map, HttpServletRequest request,
					  HttpServletResponse response) {
		System.out.println("table.....");

		//session对象
		HttpSession session = request.getSession();
		String garageName = session.getAttribute("garageName").toString();

		List<Map> list;
		if(garageName.equals("DT1")){
			list = pService.dt1();
		}else if(garageName.equals("DT2")){
			list = pService.dt2();
		}else{
			list = pService.dt3();
		}

		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");
		if (list != null) {
			System.out.println("车库数据获取成功");

			JSONArray json = JSONArray.fromObject(list);
//			System.out.println("json   " + json);
			// 将json对象转换为字符串
			String data = json.toString();
//			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("车库数据获取失败");
		}
		return null;

	}

	//图表1
	@RequestMapping(value = "/tb1", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String tb1(@RequestParam Map<String, String> map, HttpServletRequest request,
					  HttpServletResponse response) {
		System.out.println("tb1.....");
		List<String> list = pService.tb1();
		System.out.println(list);
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");

		if (list != null) {
			System.out.println("数据获取成功");

			JSONArray json = JSONArray.fromObject(list);
//			System.out.println("json   " + json);
//
//			System.out.println("sssss    "+json);
			// 将json对象转换为字符串
			String data = json.toString();
//			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("数据获取失败");
		}
		return null;

	}
	//图表2
	@RequestMapping(value = "/tb2", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String tb2(@RequestParam Map<String, String> map, HttpServletRequest request,
					  HttpServletResponse response) {
		System.out.println("tb2.....");
		List<String> list = pService.tb2();
		System.out.println(list);
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");

		if (list != null) {
			System.out.println("数据获取成功");

			JSONArray json = JSONArray.fromObject(list);
			System.out.println("json   " + json);

			System.out.println("sssss    "+json);
			// 将json对象转换为字符串
			String data = json.toString();
			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("数据获取失败");
		}
		return null;

	}
	//图表3
	@RequestMapping(value = "/tb3", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String tb3(@RequestParam Map<String, String> map, HttpServletRequest request,
					  HttpServletResponse response) {
		System.out.println("tb3.....");
		List<Map> list = pService.tb3();
		int list_len = list.size();

		//声明起始,结束时间的list
		List entry_list = new ArrayList();
		List leave_list = new ArrayList();

		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");

		if (list != null) {
			System.out.println("数据获取成功");
			for(int i=0;i<list.size();i++){
				entry_list.add(list.get(i).get("entry_time"));
				leave_list.add(list.get(i).get("leave_time"));
			}

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String start = null;
			String end = null;
			Date time1 = null;
			Date time2 = null;
			long l = 0;
			long day = 0;
			long hour = 0;
			long j1 = 0;
			long j2 = 0;
			long j3 = 0;
			long i1 = 0;
			long i2 = 0;
			//获取单个的时间
			for(int i=0;i<list.size();i++){
				start = entry_list.get(i).toString();
				end = leave_list.get(i).toString();
				try{
					time1 = df.parse(start);// 转化为date类型
					time2 = df.parse(end);// 转化为date类型
					l = time2.getTime() - time1.getTime();
					day = l / (24 * 60 * 60 * 1000);
					hour = (l / (60 * 60 * 1000) - day * 24);
//					System.out.println("您的车一共停放了:" + day + "天" + hour + "小时");
					if(day == 0){
						if(hour<4){
							j1++;
						}else if(hour<12){
							j2++;
						}else{
							j3++;
						}
					}else if(day == 1){
						i1++;
					}else{
						i2++;
					}


				}catch(Exception e){
					System.out.println("异常。。。。" +e);
				}
			}
			String[] name= {"少于四个小时","多于4个小时少于12小时","多于12个小时少于24小时","多于24小时小于48小时","多于48小时"};
			long[] data_long= {j1,j2,j3,i1,i2};

			Map name_map1= new HashMap();
			Map name_map2= new HashMap();
			Map name_map3= new HashMap();
			Map name_map4= new HashMap();
			Map name_map5= new HashMap();

			name_map1.put("name11",name[0]);
			name_map1.put("data11",data_long[0]);
			name_map2.put("name11",name[1]);
			name_map2.put("data11",data_long[1]);
			name_map3.put("name11",name[2]);
			name_map3.put("data11",data_long[2]);
			name_map4.put("name11",name[3]);
			name_map4.put("data11",data_long[3]);
			name_map5.put("name11",name[4]);
			name_map5.put("data11",data_long[4]);

			List<Map> list_sj = new ArrayList<Map>();
			list_sj.add(name_map1);
			list_sj.add(name_map2);
			list_sj.add(name_map3);
			list_sj.add(name_map4);
			list_sj.add(name_map5);

			JSONArray json = JSONArray.fromObject(list_sj);

			// 将json对象转换为字符串
			String data = json.toString();
			return data;
		} else {
			System.out.println("数据获取失败");
		}
		return null;

	}

	//图表4
	@RequestMapping(value = "/tb4", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String tb4(@RequestParam Map<String, String> map, HttpServletRequest request,
					  HttpServletResponse response) {
		System.out.println("tb4.....");
		List<Map> list = pService.tb3();
		int list_len = list.size();

		//声明起始,结束时间的list
		List entry_list = new ArrayList();


		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");

		if (list != null) {
			System.out.println("数据获取成功");
			for(int i=0;i<list.size();i++){
				entry_list.add(list.get(i).get("entry_time"));

			}

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String start = null;

			Date time1 = null;


			int hour = 0;
			long j1 = 0;
			long j2 = 0;
			long j3 = 0;
			long j4 = 0;
			long j5 = 0;
			long j6 = 0;
			//获取单个的时间
			for(int i=0;i<list.size();i++){
				start = entry_list.get(i).toString();

				try{
					time1 = df.parse(start);// 转化为date类型

					hour = time1.getHours();
//					System.out.println("hour   :"+ hour);

					if(hour > 7&&hour<11){
						j1++;
					}else if(hour >= 7&&hour<11){
						j2++;
					}else if(hour >= 11&&hour<15){
						j3++;
					}else if(hour >= 15&&hour<19){
						j4++;
					}else if(hour >= 3&&hour<7){
						j6++;
					}else{
						j5++;
					}


				}catch(Exception e){
					System.out.println("异常。。。。" +e);
				}
			}

			List<Long> list_sj = new ArrayList<Long>();
			list_sj.add(j1);
			list_sj.add(j2);
			list_sj.add(j3);
			list_sj.add(j4);
			list_sj.add(j5);
			list_sj.add(j6);

			JSONArray json = JSONArray.fromObject(list_sj);
			System.out.println("json   " + json);

			// 将json对象转换为字符串
			String data = json.toString();
			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("数据获取失败");
		}
		return null;

	}

	//图表32
	@RequestMapping(value = "/tb32", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String tb32(@RequestParam Map<String, String> map, HttpServletRequest request,
					  HttpServletResponse response) {
		System.out.println("tb32.....");

		Map maps = new HashMap();
		//session对象
		HttpSession session = request.getSession();
		maps.put("garageName",session.getAttribute("garageName").toString());

		List<Map> list = pService.tb32(maps);
		int list_len = list.size();

		//声明起始,结束时间的list
		List entry_list = new ArrayList();
		List leave_list = new ArrayList();

		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");

		if (list != null) {
			System.out.println("数据获取成功");
			for(int i=0;i<list.size();i++){
				entry_list.add(list.get(i).get("entry_time"));
				leave_list.add(list.get(i).get("leave_time"));
			}

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String start = null;
			String end = null;
			Date time1 = null;
			Date time2 = null;
			long l = 0;
			long day = 0;
			long hour = 0;
			long j1 = 0;
			long j2 = 0;
			long j3 = 0;
			long i1 = 0;
			long i2 = 0;
			//获取单个的时间
			for(int i=0;i<list.size();i++){
				start = entry_list.get(i).toString();
				end = leave_list.get(i).toString();
				try{
					time1 = df.parse(start);// 转化为date类型
					time2 = df.parse(end);// 转化为date类型
					l = time2.getTime() - time1.getTime();
					day = l / (24 * 60 * 60 * 1000);
					hour = (l / (60 * 60 * 1000) - day * 24);
//					System.out.println("您的车一共停放了:" + day + "天" + hour + "小时");
					if(day == 0){
						if(hour<4){
							j1++;
						}else if(hour<12){
							j2++;
						}else{
							j3++;
						}
					}else if(day == 1){
						i1++;
					}else{
						i2++;
					}


				}catch(Exception e){
					System.out.println("异常。。。。" +e);
				}
			}
			String[] name= {"少于四个小时","多于4个小时少于12小时","多于12个小时少于24小时","多于24小时小于48小时","多于48小时"};
			long[] data_long= {j1,j2,j3,i1,i2};

			Map name_map1= new HashMap();
			Map name_map2= new HashMap();
			Map name_map3= new HashMap();
			Map name_map4= new HashMap();
			Map name_map5= new HashMap();

			name_map1.put("name11",name[0]);
			name_map1.put("data11",data_long[0]);
			name_map2.put("name11",name[1]);
			name_map2.put("data11",data_long[1]);
			name_map3.put("name11",name[2]);
			name_map3.put("data11",data_long[2]);
			name_map4.put("name11",name[3]);
			name_map4.put("data11",data_long[3]);
			name_map5.put("name11",name[4]);
			name_map5.put("data11",data_long[4]);

			List<Map> list_sj = new ArrayList<Map>();
			list_sj.add(name_map1);
			list_sj.add(name_map2);
			list_sj.add(name_map3);
			list_sj.add(name_map4);
			list_sj.add(name_map5);

			JSONArray json = JSONArray.fromObject(list_sj);

			// 将json对象转换为字符串
			String data = json.toString();
			return data;
		} else {
			System.out.println("数据获取失败");
		}
		return null;

	}

	//图表4
	@RequestMapping(value = "/tb42", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String tb42(@RequestParam Map<String, String> map, HttpServletRequest request,
					  HttpServletResponse response) {
		System.out.println("tb42.....");

		Map maps = new HashMap();
		//session对象
		HttpSession session = request.getSession();
		maps.put("garageName",session.getAttribute("garageName").toString());

		List<Map> list = pService.tb32(maps);
		int list_len = list.size();

		//声明起始,结束时间的list
		List entry_list = new ArrayList();


		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");

		if (list != null) {
			System.out.println("数据获取成功");
			for(int i=0;i<list.size();i++){
				entry_list.add(list.get(i).get("entry_time"));

			}

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String start = null;

			Date time1 = null;


			int hour = 0;
			long j1 = 0;
			long j2 = 0;
			long j3 = 0;
			long j4 = 0;
			long j5 = 0;
			long j6 = 0;
			//获取单个的时间
			for(int i=0;i<list.size();i++){
				start = entry_list.get(i).toString();

				try{
					time1 = df.parse(start);// 转化为date类型

					hour = time1.getHours();
//					System.out.println("hour   :"+ hour);

					if(hour > 7&&hour<11){
						j1++;
					}else if(hour >= 7&&hour<11){
						j2++;
					}else if(hour >= 11&&hour<15){
						j3++;
					}else if(hour >= 15&&hour<19){
						j4++;
					}else if(hour >= 3&&hour<7){
						j6++;
					}else{
						j5++;
					}


				}catch(Exception e){
					System.out.println("异常。。。。" +e);
				}
			}

			List<Long> list_sj = new ArrayList<Long>();
			list_sj.add(j1);
			list_sj.add(j2);
			list_sj.add(j3);
			list_sj.add(j4);
			list_sj.add(j5);
			list_sj.add(j6);

			JSONArray json = JSONArray.fromObject(list_sj);
//			System.out.println("json   " + json);

			// 将json对象转换为字符串
			String data = json.toString();
//			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("数据获取失败");
		}
		return null;

	}

	// 会员
	@RequestMapping(value = "/user", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String vuser(@RequestParam Map<String, String> map, HttpServletRequest request,
					  HttpServletResponse response) {
		System.out.println("会员.....");
		List<Map> list = pService.vuser();
		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");
		if (list != null) {
			System.out.println("会员数据获取成功");

			JSONArray json = JSONArray.fromObject(list);
			System.out.println("json   " + json);
			// 将json对象转换为字符串
			String data = json.toString();
			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("会员数据获取失败");
		}
		return null;

	}
	// 历史
	@RequestMapping(value = "/history", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String history(@RequestParam Map<String, String> map, HttpServletRequest request,
						HttpServletResponse response) {
		System.out.println("历史.....");
		List<Map> list = pService.history();
		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");
		if (list != null) {
			System.out.println("历史数据获取成功");

			JSONArray json = JSONArray.fromObject(list);
			System.out.println("json   " + json);
			// 将json对象转换为字符串
			String data = json.toString();
			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("历史数据获取失败");
		}
		return null;

	}

	// 管理员
	@RequestMapping(value = "/role", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String manager(@RequestParam Map<String, String> map, HttpServletRequest request,
						  HttpServletResponse response) {
		System.out.println("管理员.....");
		List<Map> list = pService.role();
		// 设置字符编码集
		response.setContentType("text/html;charset=UTF-8"); // 设置请求以及响应的内容类型以及编码方式
		response.setCharacterEncoding("UTF-8");
		if (list != null) {
			System.out.println("管理员数据获取成功");

			JSONArray json = JSONArray.fromObject(list);
			System.out.println("json   " + json);
			// 将json对象转换为字符串
			String data = json.toString();
			System.out.println("data    " + data);
			return data;
		} else {
			System.out.println("管理员数据获取失败");
		}
		return null;

	}
	// number
	@RequestMapping(value = "/number", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String numberall(@RequestParam Map<String, String> map, HttpServletRequest request,
						HttpServletResponse response) {
		System.out.println("numberall.....");
		Map allmap = pService.numberall();

		if(allmap!= null){
			String data  = allmap.get("count(*)").toString();
			return data;
		}
		return "";
	}
	// number1
	@RequestMapping(value = "/number1", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String numberall1(@RequestParam Map<String, String> map, HttpServletRequest request,
							HttpServletResponse response) {
		System.out.println("numberall1.....");
		Map allmap = pService.numberall();

		if(allmap!= null){
			String data  = allmap.get("sum(spend)").toString();
			return data;
		}
		return "";
	}
	// number2
	@RequestMapping(value = "/number2", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String numberall2(@RequestParam Map<String, String> map, HttpServletRequest request,
							HttpServletResponse response) {
		System.out.println("numberall2.....");
		Map mapt = new HashMap();
		//session对象
		HttpSession session = request.getSession();
		mapt.put("garageName",session.getAttribute("garageName").toString());
		Map allmap = pService.numberall2(mapt);

		if(allmap!= null){
			String data  = allmap.get("count(*)").toString();
			return data;
		}
		return "";
	}
	// number3
	@RequestMapping(value = "/number3", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String numberall3(@RequestParam Map<String, String> map, HttpServletRequest request,
							 HttpServletResponse response) {
		System.out.println("numberall3.....");
		Map mapt = new HashMap();
		//session对象
		HttpSession session = request.getSession();
		mapt.put("garageName",session.getAttribute("garageName").toString());

		Map allmap = pService.numberall2(mapt);

		if(allmap!= null){
			String data  = allmap.get("sum(spend)").toString();
			return data;
		}
		return "";
	}
	}
